import Vue from "vue";
import router from "vue-router";
Vue.use(router);
const Home = () => import("views/Home");
const Category = () => import("views/Category");
const Cart = () => import("views/Cart");
const Profile = () => import("views/Profile");
const Detail = () => import("views/Detail");
const routes = [
  {
    path: "",
    redirect: "/home",
  },
  {
    path: "/home",
    component: Home,
    meta:{
      isShow:true
    }
  },
  {
    path: "/category",
    component: Category,
    meta:{
      isShow:true
    }
  },
  {
    path: "/cart",
    component: Cart,
    meta:{
      isShow:true
    }
  },
  {
    path: "/profile",
    component: Profile,
    meta:{
      isShow:true
    }
  },
  {
    path: "/detail/:id",
    component: Detail,
    meta:{
      isShow:false
    }
  },
];

export default new router({
  routes,
  mode: "history",
});
let originPush = router.prototype.push;
let originReplace = router.prototype.replace;
//重写VueRouter.prototype身上的push方法了
router.prototype.push = function (location, resolve, reject) {
  //第一个形参：路由跳转的配置对象（query|params）
  //第二个参数：undefined|箭头函数（成功的回调）
  //第三个参数:undefined|箭头函数（失败的回调）
  if (resolve && reject) {
    //push方法传递第二个参数|第三个参数（箭头函数）
    //originPush：利用call修改上下文，变为(路由组件.$router)这个对象，第二参数：配置对象、第三、第四个参数：成功和失败回调函数
    originPush.call(this, location, resolve, reject);
  } else {
    //push方法没有产地第二个参数|第三个参数
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
//重写VueRouter.prototype身上的replace方法了
router.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
